package com.coin.exchange.otcbtc.api.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.coin.exchange.otcbtc.api.OtcbtcPublicApi;
import com.coin.exchange.util.HttpUtil;

/**
 * 
 * @author duyuchong
 *
 */
@Service
public class OtcbtcPublicApiImpl implements OtcbtcPublicApi {

	@Value("${otcbtc.api.url}")
	private String URL_PREX = "https://bb.otcbtc.com";
	
	private static final String MARKETS_URL = "/api/v2/markets";
	private static final String TICKERS_URL = "/api/v2/tickers";
	private static final String ORDER_BOOK_URL = "/api/v2/order_book";
	private static final String TRADES_URL = "/api/v2/trades";
	private static final String TIMESTAMP_URL = "/api/v2/timestamp";
	private static final String KLINES_URL = "/api/v2/klines";
	private static final String KLINES_WITH_PENDING_TRADES_URL = "/api/v2/klines_with_pending_trades";
	
	@Override
	public String getMarkets() throws Exception {
		String url = URL_PREX + MARKETS_URL;
		String res = HttpUtil.httpGet(url, null);
		return res;
	}

	@Override
	public String getTickers(String marketId) throws Exception {
		String url = URL_PREX + TICKERS_URL;
		if (marketId != null) {
			url = url + "/" + marketId;
		}
		String res = HttpUtil.httpGet(url, null);
		return res;
	}

	@Override
	public String getOrderBook(String market, Integer asksLimit, Integer bidsLimit) throws Exception {
		String url = URL_PREX + ORDER_BOOK_URL;
		Map<String, Object> params = new HashMap<>();
		params.put("market", market);
		if (asksLimit != null) params.put("asks_limit", asksLimit);
		if (bidsLimit != null) params.put("bids_limit", bidsLimit);
		String res = HttpUtil.httpGet(url, params);
		return res;
	}

	@Override
	public String getTrades(String market, Integer limit, Long timestamp, String from, String to, String orderBy) throws Exception {
		String url = URL_PREX + TRADES_URL;
		Map<String, Object> params = new HashMap<>();
		params.put("market", market);
		if (limit != null) params.put("limit", limit);
		if (timestamp != null) params.put("timestamp", timestamp);
		if (from != null) params.put("from", from);
		if (to != null) params.put("to", to);
		if (orderBy != null) params.put("order_by", orderBy);
		String res = HttpUtil.httpGet(url, params);
		return res;
	}

	@Override
	public String getTimestamp() throws Exception {
		String url = URL_PREX + TIMESTAMP_URL;
		String res = HttpUtil.httpGet(url, null);
		return res;
	}

	@Override
	public String getKlines(String market, Integer limit, Integer period, Long timestamp) throws Exception {
		String url = URL_PREX + KLINES_URL;
		Map<String, Object> params = new HashMap<>();
		params.put("market", market);
		if (limit != null) params.put("limit", limit);
		if (period != null) params.put("period", period);
		if (timestamp != null) params.put("timestamp", timestamp);
		String res = HttpUtil.httpGet(url, params);
		return res;
	}

	@Override
	public String getKlinesWithPendingTrades(String market, String tradeId, Integer limit, Integer period, Long timestamp) throws Exception {
		String url = URL_PREX + KLINES_WITH_PENDING_TRADES_URL;
		Map<String, Object> params = new HashMap<>();
		params.put("market", market);
		if (tradeId != null) params.put("trade_id", tradeId);
		if (limit != null) params.put("limit", limit);
		if (period != null) params.put("period", period);
		if (timestamp != null) params.put("timestamp", timestamp);
		String res = HttpUtil.httpGet(url, params);
		return res;
	}
	
}
